package com.sysu.simulation.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.sysu.simulation.R;

import java.util.Map;

public class RecordsListAdapter extends BaseRecyclerAdapter<String> {
    private Gson gson = new Gson();

    public RecordsListAdapter(Context context) {
        super(context, 0);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        return new RecordsListViewHolder(mInflater.inflate(R.layout.item_records_list, parent, false));
    }

    @Override
    protected void onBindDefaultViewHolder(RecyclerView.ViewHolder holder, String item, int position) {
        RecordsListViewHolder vh = (RecordsListViewHolder) holder;

        Map<String, JsonElement> paramsMap = gson.fromJson(item, new TypeToken<Map<String,JsonElement>>(){}.getType());

        vh.mRecordDate.setText(paramsMap.get("time").getAsString());
    }

    private class RecordsListViewHolder extends RecyclerView.ViewHolder {
        private TextView mRecordDate, mRecordMaterial, mRecordThickness, mRecordType;

        public RecordsListViewHolder(View itemView) {
            super(itemView);
            mRecordDate = (TextView) itemView.findViewById(R.id.record_date);
            mRecordMaterial = (TextView) itemView.findViewById(R.id.record_material);
            mRecordThickness = (TextView) itemView.findViewById(R.id.record_thickness);
            mRecordType = (TextView) itemView.findViewById(R.id.record_type);
        }
    }
}
